import Vue from 'vue'
import Router from 'vue-router'
import store from '@/components/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import * as s from '@/components/utils/sessionStorage'

import index from '@/components/index'
import Login from '@/components/page/Login'
import Menu from '@/components/page/Menu'
import Role from '@/components/page/Role'
import User from '@/components/page/User'


Vue.use(Router)
const router=new Router({
  routes: [{
    path: '*',
    redirect: '/login'
  }, {
    path: '/index',
    name: 'index',
    component: index
  },{
    path: '/login',
    name: 'Login',
    component: Login
  },{
    path: '/page/Menu',
    name: 'Menu',
    component: Menu
  }, {
    path: '/page/Role',
    name: 'Role',
    component: Role
  }, {
    path: '/page/User',
    name: 'User',
    component: User
  }]
})
//捕获权限 
router.beforeEach((to, from, next) => {
  let token=s.get('token');
  NProgress.start()
  // if(to.path == "/index") {
  //   s.set('index', 0)
  //   store.dispatch('setFlagShow', true)
  //   next()
  // }
  if(to.path=="/login"){
    s.set('token', null);
    store.dispatch('setFlagShow', false)
    next();
  }else{
    if(token!=null){
      next();
      return;
    }
    store.dispatch('setFlagShow', false)
    next({path:'/login',replace:true})
  }
})
router.afterEach(() => {
  NProgress.done()
})
export default router
